﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBORpt = Texfina.BORpt.Rh;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using CrystalDecisions.Shared;

namespace RH
{
    public partial class wfAFPnetRpt : EditPage
   {


      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
         
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = FormEditAction.Edit ;
         this.SetInitialTitle("Exportar AFP net");
      }

      public override void LoadListControls()
      {
         ETablaGenDet objEDet = new ETablaGenDet();
 
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

         EEmpEstable objEEmp = new EEmpEstable();
         SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));

         objEDet.IdTabla = ConstMultitabla.MES;
         DataTable dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidMes, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);

         objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidFormaPago, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);
      }

      public override void DataToControls()
      {
          
      }

      public override void RegisterGeneralScripts()
      {
         
         StringBuilder sb = new StringBuilder();
         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);
      }

      public void RegisterEditPopupScripts()
      {

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_PRINT:
               PrintReport("");
               break;
            case ConstMenuToolbarValue.BUTTON_EXPORT:
               ExportReport("");
               break;
         }

      }

      public override void PrintReport(string key)
      {
         ReportDocument objRpt = new ReportDocument();

         DataSet ds = (DataSet)this.GetDataReport(key);

         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "No se encotraron Resultados", "");
            return;
         }

         objRpt.Load(this.MapPath(ConstSistema.ROOT_FOLDER_PATH + "/ConsoPlanilla.rpt"));
         objRpt.SetDataSource(ds.Tables[0]);


         MemoryStream ms = new MemoryStream();

         Response.Clear();

         ms = (MemoryStream)objRpt.ExportToStream(ExportFormatType.PortableDocFormat);
         objRpt.Dispose();
         objRpt = null;

         Response.AddHeader("Content-Disposition", "attachment;filename=ConsolidadoPlanilla.pdf");
         Response.Buffer = true;
         Response.ContentType = "application/pdf";

         ms.WriteTo(Response.OutputStream);
         Response.Flush();
         Response.End();

      }
      
      public override void ExportReport(string key)
      {
         DataSet ds = (DataSet)this.GetDataReport(key);
         if (ds.Tables[0].Rows.Count == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "No se encotraron Resultados", "");
            return;
         }
         ExcelHelper.ToExcel(ds, "Data.xls", Response);

      }

      public override object GetDataReport(string key)
      {

          EAFPnet objEDet = new EAFPnet();
         RHBORpt.IAFPnet objBODet = (RHBORpt.IAFPnet)WCFHelper.GetObject<RHBORpt.IAFPnet>(typeof(RHBORpt.AFPnet));
         objEDet.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
         objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objEDet.IdForPago = this.ddlidFormaPago.SelectedItem.Value;
         objEDet.IdMes = this.ddlidMes.SelectedValue;  
         DataTable dt = objBODet.GetDataAFPnet(objEDet);
         DataSet ds = new DataSet();
         ds.Tables.Add(dt);
          
         return ds;


      }

      
   }
}